package com.abc.robot01.vivemodel

import android.util.Log
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.abc.robot01.retrofit.RetrofitService
import com.abc.robot01.retrofit.bean.Condition20
import com.abc.robot01.retrofit.bean.Condition22
import com.abc.robot01.retrofit.bean.FetchDataRequest9
import com.abc.robot01.retrofit.bean.HousePlanDataDTO
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
import org.json.JSONObject

class HuXingShuJuViewModel2 : ViewModel() {

    val name1 = mutableStateOf("空")
    val customerName = mutableStateOf("小二")
    val customerPhone = mutableStateOf("123")
    val type = mutableStateOf("空 type")
    val createTime = mutableStateOf("time")
    val remark1 = mutableStateOf("remark1")

    val immutableMap = mapOf(
        1 to "原始数据",
        2 to "户型数据",
        4 to "提交渲染前数据",
        )


    var imageUrl: String=""

    /**
     * 当前选中的数据id
     */
    var housePlanDataId: String=""

    val TAG = "户型数据"
    val expanded1 =  mutableStateOf(false)
    val show =  mutableStateOf(false)
    val expanded2 =  mutableStateOf(false)
    val selected10 =  mutableStateOf("请选择")
    val selected20 =  mutableStateOf("请选择")
    val remark =  mutableStateOf("")
    // MutableStateFlow 用来保存当前的列表状态
    private val _questionList = MutableStateFlow<List<HousePlanDataDTO>>(emptyList())
    val questionList: StateFlow<List<HousePlanDataDTO>> = _questionList
    val name = mutableStateOf("")

    private val _designerList = MutableStateFlow<List<Pair<String, String>>>(emptyList())
    val designerList: StateFlow<List<Pair<String, String>>> = _designerList
    val designPackageId = mutableStateOf("")
    var housePlanId = ""
    var selectedIndex = mutableStateOf(-1)

    fun reset(){
        name.value = ""
        remark.value = ""
        selectedIndex.value = -1 ;
        showDetailButton.value = false
        selected2.value = false
    }

    //当前选择的创建者
    val selectedDesigner = mutableStateOf("")

    //数据详情
    val showDetailButton =  mutableStateOf(false)
    val selected2 =  mutableStateOf(false)
    var thumbnail=""

        fun loadDataByPhone(
            phone: String
        ) {
            viewModelScope.launch {
                _questionList.value = emptyList()
                val res = RetrofitService.instance.getHousePlanDataByPhone(
                    accessToken = Robot.accessToken,
                    userId = Robot.userId,
                    companyId = Robot.companyId,
                    request = FetchDataRequest9(
                        Condition22(
                            name = name.value,
                            remarks = remark.value,
                            customerPhone = phone,
                        )
                    )
                ).string()

                Log.e(TAG, "RetrofitService res:$res")
                val obj = JSONObject(res)
                val code = obj.getInt("code")
                Log.e(TAG, "RetrofitService code:$code")
                val list = obj.getJSONObject("data").getJSONArray("list")
                Log.e(TAG, "RetrofitService list:${list.length()}")

                val itemList = mutableListOf<HousePlanDataDTO>()

                // 如果你只想打印特定字段，比如 "name"
                for (i in 0 until list.length()) {
                    val item = list.getJSONObject(i)
                    val id = item.optString("id", "No id") // 默认值为 "No Name"
                    val type = item.optString("type", "type 123") // 默认值为 "No Name"
                    val customerName = item.optString("customerName", "/") // 默认值为 "No Name"
                    val customerPhone = item.optString("customerPhone", "/") // 默认值为 "No Name"
                    val createTime = item.optString("createTime", "/") // 默认值为 "No Name"
                    val name = item.optString("name", "No Name") // 默认值为 "No Name"
                    val remarks = item.optString("remarks", "/") // 默认值为 "No Name"
                    Log.e(TAG, "RetrofitService name:$name")
                    housePlanId = item.optString("housePlanId", "") // 默认值为 "No Name"
                    Log.e(TAG, "RetrofitService remarks:$remarks")
                    val imageUrl = item.optString("imageUrl", "No imageUrl") // 默认值为 "No Name"
                    Log.e(TAG, "RetrofitService imageUrl:$imageUrl")
                    itemList.add(
                        HousePlanDataDTO(
                            id = id,
                            name = name,
                            imageUrl = imageUrl,
                            remarks = remarks,
                            createTime = createTime,
                            customerName = customerName,
                            customerPhone = customerPhone,
                            type = type
                        )
                    )
                }
                _questionList.value = itemList
            }


        }












}
